package handler

import (
	"car-trade-mgr/model"
	"github.com/gin-gonic/gin"
)

func UserAndRole(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.rid = roles.id
		WHERE
		  user_roles.uid = 1
	*/
	var role []model.Role

	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid = ?", uid).
		Debug().
		Find(&role)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"table": role,
	})
}
func RoleAndNode(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.nid=nodes.id
		WHERE
		  role_nodes.rid=1
	*/
	var role []model.Node
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.nid=nodes.id").
		Where("role_nodes.rid=?", rid).
		Debug().
		Find(&role)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"table": role,
	})
}
